package com.px.plan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.px.common.core.domain.model.LoginUser;
import com.px.common.utils.MinioUtil;
import com.px.common.utils.SecurityUtils;
import com.px.device.service.IDroneInfoService;
import com.px.plan.domain.PlanAction;
import com.px.plan.domain.PlanActionRecord;
import com.px.plan.domain.PlanActionScheme;
import com.px.plan.mapper.PlanActionRecordMapper;
import com.px.plan.service.IPlanActionRecordService;
import com.px.plan.service.IPlanActionSchemeService;
import com.px.plan.service.IPlanActionService;
import com.px.system.domain.DroneInfo;
import com.px.plan.domain.PlanActionRecordMedia;
import com.px.plan.mapper.PlanActionRecordMediaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * 飞行行动记录Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class PlanActionRecordServiceImpl implements IPlanActionRecordService 
{
    @Autowired
    private PlanActionRecordMapper planActionRecordMapper;
    @Autowired
    private IPlanActionService actionService;
    @Autowired
    private IDroneInfoService droneInfoService;
    @Autowired
    private IPlanActionSchemeService planActionSchemeService;
    @Autowired
    private PlanActionRecordMediaMapper planActionRecordMediaMapper;

    @Value("${minio.bucketName}")
    private String bucketName;
    /**
     * 查询飞行行动记录
     * 
     * @param id 飞行行动记录主键
     * @return 飞行行动记录
     */
    @Override
    public PlanActionRecord selectPlanActionRecordById(Long id)
    {
        return planActionRecordMapper.selectPlanActionRecordById(id);
    }

    /**
     * 查询飞行行动记录列表
     * 
     * @param planActionRecord 飞行行动记录
     * @return 飞行行动记录
     */
    @Override
    public List<PlanActionRecord> selectPlanActionRecordList(PlanActionRecord planActionRecord)
    {
        return planActionRecordMapper.selectPlanActionRecordList(planActionRecord);
    }

    /**
     * 新增飞行行动记录
     * 
     * @param planActionRecord 飞行行动记录
     * @return 结果
     */
    @Override
    public int insertPlanActionRecord(PlanActionRecord planActionRecord)
    {
        return planActionRecordMapper.insertPlanActionRecord(planActionRecord);
    }

    /**
     * 修改飞行行动记录
     * 
     * @param planActionRecord 飞行行动记录
     * @return 结果
     */
    @Override
    public int updatePlanActionRecord(PlanActionRecord planActionRecord)
    {
        return planActionRecordMapper.updatePlanActionRecord(planActionRecord);
    }

    /**
     * 批量删除飞行行动记录
     * 
     * @param ids 需要删除的飞行行动记录主键
     * @return 结果
     */
    @Override
    public int deletePlanActionRecordByIds(Long[] ids) throws Exception {
        //删除minio记录
        removeMinio(ids);
        return planActionRecordMapper.deletePlanActionRecordByIds(ids);
    }

    @Async
    public void removeMinio(Long[] ids) throws Exception {
        for (Long id : ids) {
            PlanActionRecordMedia planActionRecordMedia = new PlanActionRecordMedia();
            planActionRecordMedia.setRecordId(id);
            List<PlanActionRecordMedia> parmList = planActionRecordMediaMapper.selectPlanActionRecordMediaList(planActionRecordMedia);
            for (PlanActionRecordMedia parm : parmList) {
                MinioUtil.removeFile(bucketName, parm.getMediaUrl().replace(bucketName+"/",""));
            }
        }
    }
    /**
     * 删除飞行行动记录信息
     * 
     * @param id 飞行行动记录主键
     * @return 结果
     */
    @Override
    public int deletePlanActionRecordById(Long id)
    {
        return planActionRecordMapper.deletePlanActionRecordById(id);
    }

    @Override
    public Long insertPlanActionRecord(Long actionId, String sn) {
        PlanActionRecord record = new PlanActionRecord();
        PlanAction planAction = actionService.selectPlanActionById(actionId);
        DroneInfo droneInfo = droneInfoService.selectDroneInfoBySn(sn);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Assert.notNull(droneInfo,"未注册的无人机SN");
        Assert.notNull(planAction,"不存在的行动ID");
        PlanActionScheme actionScheme = planActionSchemeService.selectPlanActionSchemeById(planAction.getSchemeId());

        record.setActionId(actionId);
        record.setActionName(planAction.getName());
        record.setActionStatus(planAction.getStatus());
        if (null != actionScheme) {
            record.setSchemeId(actionScheme.getId());
            record.setSchemeName(actionScheme.getName());
        }
        record.setPilotId(loginUser.getUserId());
        record.setPilotName(loginUser.getUser().getNickName());
        record.setFlightTime(0L);
        record.setFlightDistance(0);
        record.setDroneId(droneInfo.getId());
        record.setDroneSn(sn);
        record.setDroneName(droneInfo.getName());
        record.setFlightTimeBegin(new Date());
        insertPlanActionRecord(record);
        return record.getId();
    }

    @Override
    public Long getRecordId(Long actionId, String droneSn) {
        PlanActionRecord record = new PlanActionRecord();
        record.setActionId(actionId);
        record.setDroneSn(droneSn);
        List<PlanActionRecord> records = selectPlanActionRecordList(record);
        if (CollectionUtil.isNotEmpty(records)) {
            return records.get(0).getId();
        } else {
            return insertPlanActionRecord(actionId, droneSn);
        }
    }

    @Override
    public Long getRecordIdByActionAndSn(Long actionId, String droneSn) {
        PlanActionRecord record = new PlanActionRecord();
        record.setActionId(actionId);
        record.setDroneSn(droneSn);
        List<PlanActionRecord> planActionRecords = selectPlanActionRecordList(record);
        if (CollectionUtil.isNotEmpty(planActionRecords)) {
            return planActionRecords.get(0).getId();
        }
        return null;
    }
}
